/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firefighter;

import javax.microedition.lcdui.*;

/**
 *
 * @author Shawn
 */
public class MainMenu extends Canvas {

    private Firefighter midlet;
    private Image menuImage, menuSelectorImage;
    private int index = 0;
    private int menuSelectorX = getWidth() / 2 - 3;
    private int menuSelectorY = 76;
    private int menuIntervalY = 31;

    public MainMenu(Firefighter m) {
        midlet = m;
        setFullScreenMode(true);
        menuSelectorImage = Firefighter.createImage("/MainMenuSelector.png");
        menuImage = Firefighter.createImage("/Mainmenu.png");
    }

    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();
        g.setColor(0x00FFFFFF); // white
        g.fillRect(0, 0, width, height);
        g.setColor(0x00FF0000); // red
        g.drawRect(1, 1, width - 3, height - 3); // red border one pixel from edge

        g.drawImage(menuImage, width / 2, height / 2, Graphics.VCENTER | Graphics.HCENTER);
        g.drawImage(menuSelectorImage, menuSelectorX, menuSelectorY, Graphics.VCENTER | Graphics.HCENTER);
    }

    protected void keyPressed(int keyPressed) {
        int gameAction = getGameAction(keyPressed);

        if (gameAction == 0)
        {
            gameAction = keyPressed;
        }
        
        switch (gameAction) {
            case Canvas.KEY_NUM2:
            case Canvas.UP:
                moveUp();
                break;
            case Canvas.KEY_NUM8:
            case Canvas.DOWN:
                moveDown();
                break;
            case Canvas.KEY_NUM5:
            case Canvas.FIRE:
                menuSelect();
                break;
        }
    }

    protected void keyRepeated(int keyRepeated) {
        keyPressed(keyRepeated);//same response as key pressed
    }

    void moveUp() {
        index--;
        if (index < 0) {
            menuSelectorY += 4 * menuIntervalY;
            index = 4;
        } else {
            menuSelectorY -= menuIntervalY;
        }
        repaint();
    }

    void moveDown() {
        index++;
        if (index > 4) {
            menuSelectorY -= 4 * menuIntervalY;
            index = 0;
        } else {
            menuSelectorY += menuIntervalY;
        }
        repaint();
    }

    void menuSelect() {
        switch (index) {
            case 0:
                midlet.mainMenuNewGame();
                break;
            case 1:
                midlet.mainMenuContinue();
                break;
            case 2:
                midlet.mainMenuHelp();
                break;
            case 3:
                midlet.mainMenuExit();
                break;
            case 4:
                midlet.mainMenuHiScores();
                break;
        }
    }

    private void clearScreen(Graphics g) {
        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}
